#!/usr/bin/env python
import roslib; roslib.load_manifest("ratsim")
import rospy
from lingodroids_bridge.msg import Word
import httplib
import os
import time
import threading
import sys

words = [];

class iRat:
	def __init__(self, topic, ip):
		self.ip = ip
		self.topic = topic
		rospy.Subscriber(topic, Word, self.callback)
		self.voice = "hy-west"

	def callback(self, data):
		words.append((self, data.string))
    
	def speak(self, word):
		if word == "reqwor": word = "where should we meet?"
		if word == "ackwor": word = "ok";
		os.system("ssh " + self.ip + " 'echo \"irat\" | sudo -S espeak -a 200 -v "+self.voice+" \""+word+"\"' ")
		
	def consume_queue(self):
		while True:
			if len(words) == 0: continue
			irat = words[0][0];
			print irat.topic + " says: ... something "
			irat.speak(words.pop(0)[1])
		
if __name__ == '__main__':
	
	rospy.init_node("speaker", anonymous=True)
	#rospy.Subscriber("/lingodroid0_lingodroid1/words", Word, callback0)
	#rospy.Subscriber("/lingodroid1_lingodroid0/words", Word, callback1)
	
	irats = [iRat("lingodroid0_lingodroid1/words", "irat@192.168.1.32"),
		iRat("lingodroid1_lingodroid0/words", "irat@192.168.1.33")]
	
	threading.Thread(None, irats[0].consume_queue).start()
	
	threading.Thread(None, rospy.spin).start();
	
	while True:
		time.sleep(10);

